<?php

class ProjectController extends App_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        
    }
    
    public function listAction()
    {
		$top = $this->_getParam('top', 0);
		$limit = 0; $offset = 0;
		$projects = new Projects();
		if ($top == 0) {
    		$projects = $projects->getAll($limit, $offset);
		} 
		else {
    		$projects = $projects->getTopContesters($top);
		}		    	

		$user = Doctrine::getTable("Users")->find($this->getUserId());
		$allow_vote = $user->allow_vote;
		
		if ($projects->count() > 0) {
			$this->view->projects = $projects;
			$this->view->allow_vote = $allow_vote;
		}
    }
    
    public function viewAction()
    {
    	// retrieve project
		$id = $this->_getParam('id', 0);    	
		$project = Doctrine::getTable("Projects")->find($id);
    	
		// if no project with that id then go to the runnig election screen
		if (!$project) {
			// redirect to runnig election screen
			$this->_redirector->gotoSimple("index", "election");
			return;
		}      

		// retrieve project pictures
		$pictures = Doctrine::getTable("ProjectPictures")->findById_project($id);
		
		$this->view->project = $project;
		$this->view->pictures = $pictures;
    }
    
}

